﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Simple.Tools
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	public static class MathExtension
	{
		/// <summary>
		/// 地球半径km
		/// </summary>
		private static double EarthR = 6371.0;
		/// <summary>
		/// Haversine公式
		/// d=2*R*arcsin (开根号(sin (lat2-lat1)平方 + cos(lon1)*cos(lon2)*(平方(cos(lon2-lon1))))
		/// </summary>
		/// <param name="lat1">纬度</param>
		/// <param name="lon1">经度</param>
		/// <param name="lat2">纬度</param>
		/// <param name="lon2">经度</param>
		/// <returns></returns>
		public static double GetDistance(double lat1, double lon1,double lat2,double lon2)
		{
			//精度
			double y = DegreesToRadians(lon1);
			//纬度
			double x = DegreesToRadians(lat1);
			double y1 = DegreesToRadians(lon2);
			double x1 = DegreesToRadians(lat2);

			//精度
			double dy = y1 - y;
			//纬度
			double dx = x1 - x;

			double a = Math.Pow(Math.Sin(dx / 2), 2) + Math.Cos(x1) * Math.Cos(x) * Math.Pow(Math.Sin(dy / 2), 2);

			double c = 2 * Math.Asin(Math.Sqrt(a));

			return  c * EarthR;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="lat1"></param>
		/// <param name="lon1"></param>
		/// <param name="lat2"></param>
		/// <param name="lon2"></param>
		/// <returns></returns>
		public static double GetDistance(decimal lat1, decimal lon1, decimal lat2, decimal lon2)
		{
			return GetDistance((double)lat1,(double)lon1,(double)lat2,(double)lon2);
		}
		/// <summary>
		/// 角度转换为弧度
		/// </summary>
		/// <param name="degrees"></param>
		/// <returns></returns>
		public static double DegreesToRadians(double degrees)
		{
			return degrees * (Math.PI / 180.0);
		}
	}
}
